clear all
set more off
set mem 100m
tempfile temp

/* 

This dofile categorizes preferences for democracy variables using raw data from ENAHO's module on democracy  (gobernabilidad democracia y transparencia, modulo 85)


*/

local data ../data/enaho
local working ../working

** 2004-2006
forvalues y = 2004/2006 {
	use `data'/`y'/enaho01b-`y'-1, clear
	
	gen year = `y'

	if `y' == 2004 {
		replace p15 = 6 - p15
	}
	
	tab p18
	
	recode p15 (1 2 5 = 0) (3 4 = 1), gen(dem_matters0)
	recode p18 (1 2 5 = 0) (3 4 = 1), gen(dem_works)
	recode p17 (1 = 1) (else = 0), gen(gov_auto0)
	recode p17 (2 = 1) (else = 0), gen(gov_mili)
	recode p17 (3 = 1) (else = 0), gen(gov_demo0)
	recode p17 (4 = 1) (else = 0), gen(gov_exper)

	destring conglome vivienda hogar codperso, replace
	keep conglome vivienda hogar codperso year dem_* gov_* 
	
	save dem_`y', replace
}


** 2007-2016
forvalues y = 2007/2016 {
	use `data'/`y'/enaho01b-`y'-1, clear

	gen year = `y'
	keep conglome vivienda hogar codperso year p3 p6 p7 p9
	tab p7

	recode p6 (1 2 5 = 0) (3 4 = 1), gen(dem_matters1)
	recode p7 (1 2 5 = 0) (3 4 = 1), gen(dem_works)
	recode p3 (1 = 1) (2 . = 0), gen(dem_knows)
	recode p9 (1 = 1) (2/4 . = 0), gen(gov_demo1)
	recode p9 (2 = 1) (1 3/4 . = 0), gen(gov_auto1)
	recode p9 (3 = 1) (1 2 4 . = 0), gen(gov_same)
	
	destring conglome vivienda hogar codperso, replace
	keep conglome vivienda hogar codperso year dem_* gov_* 
	
	save dem_`y', replace
}

use dem_2004, clear
forvalues x = 2005/2016 {
	append using dem_`x'
}	

forvalues x = 2004/2016 {
	erase dem_`x'.dta
}	

** SAVE
compress
save `working'/democracy, replace

